package com.xilihui.xlh.business.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;

import com.xilihui.xlh.R;
import com.xilihui.xlh.business.adapters.BasePagerAdapter;
import com.xilihui.xlh.business.entities.CartListEntity;
import com.xilihui.xlh.business.entities.TakeGoodsEntity;
import com.xilihui.xlh.business.fragments.TakeGoodsFragment;
import com.xilihui.xlh.business.requests.MyAppointmentRequest;
import com.xilihui.xlh.core.app.ToolBaseCompatActivity;
import com.xilihui.xlh.core.http.DoTransform;
import com.xilihui.xlh.core.http.HttpSubscriber;
import com.xilihui.xlh.core.util.LogUtil;
import com.xilihui.xlh.core.util.ToastUtil;
import com.xilihui.xlh.core.util.YEventBuses;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;


/**
 * Created by Administrator on 2018/5/28.
 */

public class TakeGoodsActivity extends ToolBaseCompatActivity{
    
    @BindView(R.id.tablayout)
    TabLayout tabLayout;
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    @BindView(R.id.tv_goodNum)
    TextView tv_goodNum;
    
    Intent intent;
    
    int page = 1;
    String id = "";
    String cate_id = "";
    String wa_id = "";
    
    @OnClick(R.id.iv_toolbar_left)
    public void back(){
        finish();
    }
    
    @OnClick(R.id.tv_toolbar_right)
    public void webill(){
        intent = new Intent(TakeGoodsActivity.this,MyChecklistActivity.class);
        intent.putExtra("wa_id",wa_id);
        intent.putExtra("agent_id",id);
        startActivity(intent);
    }

    @OnClick(R.id.iv_checklist)
    public void checklist(){
        intent = new Intent(TakeGoodsActivity.this,ChecklistActivity.class);
        intent.putExtra("id",id);
        intent.putExtra("wa_id",wa_id);
        startActivity(intent);
    }
    
    @Override
    public int getLayout() {
        return R.layout.activity_take_goods;
    }

    @Override
    public String initTitle() {
        return null;
    }

    @Override
    public void initUI(@Nullable Bundle savedInstanceState) {
        setToolTitle("仓库领货");
        setToolRightText("我的清单");
        
        id = getIntent().getStringExtra("id");
        wa_id = getIntent().getStringExtra("wa_id");
        
    }

    @Override
    public void initData() {
        getData();
        getNum();
    }
    
    public void getData(){
        MyAppointmentRequest.takeGoods(this,id,cate_id,page,wa_id)
                .compose(DoTransform.<TakeGoodsEntity>applyScheduler(this,true))
                .compose(this.<TakeGoodsEntity>bindToLifecycle())
                .subscribe(new HttpSubscriber<TakeGoodsEntity>(this) {
                    @Override
                    protected void onSuccess(final TakeGoodsEntity entity) {
                        if(page==1){
                            
                            final ArrayList<TakeGoodsEntity.CategoriesBean> top = (ArrayList<TakeGoodsEntity.CategoriesBean>) entity.getCategories();
                            
                            if(top.size()>0){

                                ArrayList<String> titles = new ArrayList<>();
                                for (int i=0;i<top.size();i++){
                                    titles.add(top.get(i).getMobile_name());
                                }
                                
                                
                                BasePagerAdapter basePagerAdapter = new BasePagerAdapter(getSupportFragmentManager(),
                                        TakeGoodsActivity.this,titles);
                                
                                basePagerAdapter.setItemFragment(new BasePagerAdapter.getItemFragment() {
                                    @Override
                                    public Fragment getItem(int position) {
                                        TakeGoodsFragment fragment = new TakeGoodsFragment();
                                        Bundle bundle = new Bundle();
                                        bundle.putString("id",id);
                                        bundle.putString("cate_id",top.get(position).getId());
                                        bundle.putString("wa_id",wa_id);
                                        fragment.setArguments(bundle);
                                        return fragment;
                                    }
                                });
                                
                                viewPager.setAdapter(basePagerAdapter);
                                tabLayout.setupWithViewPager(viewPager);
                            }
                            
                            
                        }
                    }

                    @Override
                    protected void onFail(String msg) {

                    }
                });
        
    }
    
    public void getNum(){
        MyAppointmentRequest.cartlist(this,id,wa_id)
                .compose(DoTransform.<CartListEntity>applyScheduler(this,false))
                .compose(this.<CartListEntity>bindToLifecycle())
                .subscribe(new HttpSubscriber<CartListEntity>(this) {
                    @Override
                    protected void onSuccess(CartListEntity entity) {
                        int num = entity.getGoods().size();
                        if(num>0){
                            tv_goodNum.setText(num+"");
                            tv_goodNum.setVisibility(View.VISIBLE);
                        }else {
                            tv_goodNum.setVisibility(View.INVISIBLE);
                        }
                    }

                    @Override
                    protected void onFail(String msg) {
                        ToastUtil.toastShortNegative(msg);
                    }
                });
    }

    @Override
    public void onEventMainThread(YEventBuses.Event event) {
        super.onEventMainThread(event);
        if(event.is("joinGoods")||event.is("pay")){
            LogUtil.i("mylog","刷新购物车");
            getNum();
        }
    }
}
